//
//  RTJHBJXController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/8.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJHBJXController.h"
#import "RTJLendCell.h"
#import "RTJLendController.h"
#import "RTJTabBarController.h"

@interface RTJHBJXController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation RTJHBJXController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.estimatedRowHeight=300;
    [self.tableView reloadData];
    
    if (_isUserCenter) {
        
        [self loadData];
    }
    
}

-(void)loadData{
    
    
    [SVProgressHUD  showWithStatus:nil];
    
    if(self.hongbaoArr.count == 0){
        
        [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
    }
    
    [RTJNetWork asyncRequestWithURL:@"user/get_voucher" params:nil method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        [self.view removeEmptyView];

        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                self.hongbaoArr = data[@"data"][@"ecv_list"];
                
                if (self.hongbaoArr.count == 0) {
                    
                    [self.view showEmptyViewWithStr:@"您暂无可用红包" image:[UIImage imageNamed:@"kbylogo"] callback:^{
                        
                        [self loadData];
                    }];
                    
                }else{
                    
                    [self.tableView reloadData];

                }
                
                
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    
                    [self loadData];
                }];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                
                [self loadData];
            }];
        }
        
    }];
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _hongbaoArr.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJLendCell";
    
    RTJLendCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"RTJLendCell" owner:self options:nil][2];
    }
    
    NSInteger isUsed = [_hongbaoArr[indexPath.row][@"can_use_count"] integerValue];//是否使用
    NSInteger out_time = [_hongbaoArr[indexPath.row][@"out_time"] integerValue];//是否过期

    cell.userInteractionEnabled = out_time>0&&isUsed>0 ? YES : NO;
    cell.cardImg.image = out_time>0&&isUsed>0 ? [UIImage imageNamed:@"kqb_hbljsy"]:[UIImage imageNamed:@"kqb_hbhuise"];
    cell.cardTitle.text =  [NSString stringWithFormat:@"%@",_hongbaoArr[indexPath.row][@"name"]];
    cell.cardMoneyLab.text = [NSString stringWithFormat:@"%@",_hongbaoArr[indexPath.row][@"money_format"]];
    cell.smallLab.text = @"元";
    cell.useCountLab.text = [NSString stringWithFormat:@"该红包可用次数:%@次",_hongbaoArr[indexPath.row][@"can_use_count"]];
    
    NSString *minMoney = [NSString stringWithFormat:@"投资金额≥%@元",_hongbaoArr[indexPath.row][@"min_money_format"]];
    cell.touzhiLab.text =minMoney.length!=0 ? minMoney:[NSString stringWithFormat:@"投资金额>0元"];
    cell.useLab.text = @"不可与红包叠加使用";
    
    cell.dateLab.text = [NSString stringWithFormat:@"有效期至：%@",_hongbaoArr[indexPath.row][@"end_time_format"]];
    cell.dateLab.textColor = out_time>0&&isUsed>0 ? [UIColor colorForHex:@"#333333"] :[UIColor colorForHex:@"#CCCCCC"];
    
    if (out_time>0) {
    
        cell.usedLab.text = isUsed>0 ? @"立即使用" :@"已使用";

    }else{
        
        cell.usedLab.text =@"已过期";

    }
    
    cell.usedLab.textColor =out_time>0&&isUsed>0 ? [UIColor colorForHex:@"#FD3834"] :[UIColor colorForHex:@"#CCCCCC"];
   
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_isUserCenter) {
        
        //                for (UIViewController *controller in self.navigationController.viewControllers) {
//
//                    if ([controller isKindOfClass:[RTJLendController class]]) {
//
//                        RTJLendController *vc = (RTJLendController *)controller;
//
//                        [self.navigationController popToViewController:vc animated:YES];
//
//                    }
//
//                }
        
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        
        RTJTabBarController *tabViewController = (RTJTabBarController *) appDelegate.window.rootViewController;
        
        [tabViewController setSelectedIndex:1];

        
    }else{
        
          [self.delegate hongBaoName:[NSString stringWithFormat:@"%@",_hongbaoArr[indexPath.row][@"name"]] money:[_hongbaoArr[indexPath.row][@"money"] integerValue] cardId:[_hongbaoArr[indexPath.row][@"id"] integerValue] min_money:[_hongbaoArr[indexPath.row][@"min_money"] integerValue]];
        
        [self.navigationController popViewControllerAnimated:YES];

    }
    
    
}

@end
